#!/usr/bin/env python
#coding=utf-8

import os
import PUBLICMETHOD
import re
import platform


def getFilePath(folderPath):	
	if platform.system() == 'Windows':
		filePath = folderPath + "\\tags.dat" 

	elif platform.system() == 'Linux':
		filePath = folderPath + "/tags.dat"

	return filePath

def handleDAT(filePath):
	correctContent = []
	lineContent = PUBLICMETHOD.ReadFile.readDAT(filePath)
	isFirstLine = True
	#print sys.getsizeof(lineContent)
	for line in lineContent:
		if isFirstLine == True:
			correctContent.append(line)
			isFirstLine = False
		#11029	mitochondrion inheritance	4	http://purl.obolibrary.org/obo/GO_0000001	5
		#54940040	http://ihtsdo.org/snomedct/anatomy#361408001	4	Lower anterior intercostal arteries	3
		else:
			result1 = re.match(r'(\d)+\t(\S|\s)*\t\d\t(http://(\S)+|null)*\t\d', line)
			result2 = re.match(r'(\d)+\t(http://(\S)+|null)*\t\d\t(\S|\s)*\t\d', line)
			#if the content of line is correct
			if result1:
				correctContent.append(line)
			elif result2:
				tagData = line.split('\t')
				correctLine = tagData[0] + '\t' + tagData[3] + '\t' + tagData[2] + '\t' + tagData[1] + '\n'
			else:
				pause = raw_input("pauses")
				print line
	#print id(tagDataList)
	del lineContent
	PUBLICMETHOD.ReadFile.closeFile()
	return correctContent

def writeDAT(content):
	PUBLICMETHOD.WriteFile.writeDAT("tags_1.dat", content)
	PUBLICMETHOD.WriteFile.closeFile()

if __name__ == '__main__':
	BASE_DIR = os.getcwd()
	writeDAT(handleDAT(getFilePath(BASE_DIR)))
